--[[
   @brief    2-factor authentication
   @author   senpingtian
   @version  1.0
   @date     2019-07-16
--]]

login_auth_verify_view = {
    dialog_ptr = nil;
    dialog_ctrl_id = Tenio.INVALID_CONTROLID;
    webctrl_ptr = nil;
    webctrl_id = Tenio.INVALID_CONTROLID;
    page_url = LoadStr("LOGIN_TWOFA_VERIFY_URL");

    kWebCtrlPath = "webContentCtrl";
    kLoadingCtrlPath = "pnlLoading";
};

function login_auth_verify_view:init()
    base_utility.log("[login_auth_verify_view] init.");
    comm_center.reg_service("i_service_show_login_auth_verify_window", 
                            "login_auth_verify_view:on_show_svr");
end;

function login_auth_verify_view:uninit()
    base_utility.log("[login_auth_verify_view] uninit.");
end;

function login_auth_verify_view:on_show_svr()
    self:show(true);
end;

function login_auth_verify_view:create_dialog()
    if nil ~= self.dialog_ptr then
        return true;
    end

    self.dialog_ctrl_id = layout_mgr.restore_layout("login\\AuthVerify.tpf");

    if Tenio.INVALID_CONTROLID == self.dialog_ctrl_id then
        base_utility.log("[login_auth_verify_view] restore layout failed.", true);
        return false;
    end

    self.dialog_ptr = ui_utility.get_ctrl_ptr_by_id(self.dialog_ctrl_id);
    if nil == self.dialog_ptr then
        base_utility.log("[login_auth_verify_view] get dialog failed.", true);
        return false;
    end

    local parent_id = nil;
    if nil ~= TLoginView.pDlgLogin then
        parent_id = TLoginView.pDlgLogin:GetControlID();
    end
    self:move_to_center(parent_id);

    self.webctrl_id = ui_utility.get_ctrl_id_by_path(self.dialog_ctrl_id, self.kWebCtrlPath);
    self.webctrl_ptr = ui_utility.get_ctrl_ptr_by_id(self.webctrl_id);
    if nil == self.webctrl_ptr then
        base_utility.log("[login_auth_verify_view] get webctrl failed.", true);
        return false;
    end

    ui_utility.setproperty_by_id(self.webctrl_id, "OnNavigateFailed", 
                                "login_auth_verify_view:on_web_navigate_failed");
    ui_utility.setproperty_by_id(self.webctrl_id, "OnNavigateComplete", 
                                "login_auth_verify_view:on_web_navigate_complete");

    self:load_web_page();

    return true;
end;

function login_auth_verify_view:show_control(ptr, show)
    if nil == ptr then
        return;
    end
    if show then
        ptr:Show(TRUE);
    else
        ptr:Show(FALSE);
    end
end;

function login_auth_verify_view:show(show_view)
    base_utility.log("[login_auth_verify_view] show view.");
    login_auth_verify_model.reset();
    if self:create_dialog() then
        if true == show_view then
            self:show_control(self.dialog_ptr, true);
            self.dialog_ptr:BringToTop();
            ui_utility.do_fake_modal_by_id(self.dialog_ctrl_id);
        else
            self:show_control(self.dialog_ptr, false);
        end
    end
end;

function login_auth_verify_view:load_web_page()
    if nil == self.webctrl_ptr then
        return;
    end
    local url_str, str_holder = U8toCUIStr(self.page_url);
    self.webctrl_ptr:Navigate(url_str);
end;

function login_auth_verify_view:on_web_navigate_complete()
    local loading_panel_ptr = ui_utility.get_ctrl_ptr_by_path(self.dialog_ctrl_id,
                                                              self.kLoadingCtrlPath);
    self:show_control(loading_panel_ptr, false);
end;

function login_auth_verify_view:on_web_navigate_failed()
    base_utility.log("[login_auth_verify_view] web nav failed.", true);
end;

function login_auth_verify_view:on_show_dialog()
    if nil ~= self.webctrl_ptr then
        self.webctrl_ptr:SetFocus();
    end
end

function login_auth_verify_view:on_pos_changed()
end;

function login_auth_verify_view:move_to_center(parent_id)
    if self.dialog_ptr == nil then return end;

    if nil ~= parent_id then
        local ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil ~= ptr then
            local rect_parent = ptr:GetRect(TRUE);
            local rect_dialog = self.dialog_ptr:GetRect(TRUE);
            local ww = rect_dialog.right - rect_dialog.left;
            local wh = rect_dialog.bottom - rect_dialog.top;
            rect_dialog.left = rect_parent.left + (rect_parent.right - rect_parent.left - ww) / 2;
            rect_dialog.top = rect_parent.top + (rect_parent.bottom - rect_parent.top - wh) / 2;
            rect_dialog.right = rect_dialog.left + ww;
            rect_dialog.bottom = rect_dialog.top + wh;
            self.dialog_ptr:MoveEx(rect_dialog);
            return;
        end
    end

    self.dialog_ptr:MoveToCenter();
end;

function login_auth_verify_view:on_close()
    if ui_utility.is_visible(self.dialog_ctrl_id) then
        ui_utility.end_fake_modal_by_id(self.dialog_ctrl_id);
        layout_mgr.delay_remove_ctrl(self.dialog_ctrl_id);
        self.dialog_ptr = nil;
        self.dialog_ctrl_id = Tenio.INVALID_CONTROLID;
        self.webctrl_ptr = nil;
        self.webctrl_id = Tenio.INVALID_CONTROLID;
    end
end;


login_auth_verify_view.init();